import { _decorator, Animation, Collider2D, Contact2DType, find, IPhysics2DContact, Node, RigidBody2D, v2, Vec2 } from 'cc';
import { Monster } from '../../scripts/Monster';
import { MainLlogic } from '../../scripts/MainLlogic';
const { ccclass, property } = _decorator;


enum Monster1ani{
    'monster1_idle',
    'monster1_run'
}

@ccclass('Monster1')
export class Monster1 extends Monster{

    @property({type:Node})
    private player:Node = null

    @property({type:Vec2})
    velocity:Vec2 = v2(-20,0) 

    /**
     * 播放动画编号，0为默认播放动画
     */
    aninum = 0
    /**
     * 攻击指示，默认false
     */
    atk:boolean = false


    private ani:Animation

    /**
     * 怪物节点初始化
     * @param monsterpoolnum 节点池编号
     */
    initialize(monsterpoolnum: number): void {
        this.monsterpoolnum = monsterpoolnum
        this.atk =false
        this.setani(0)
    }


    start() {
        this.ani = this.node.getComponent(Animation)
        this.node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null){
        if(otherCollider.group == 2){
            this.recovery()
        }
        
    }

    setani(anim){
        if(this.aninum == anim) return
        this.aninum = anim
        this.ani.play(Monster1ani[this.aninum])
    }

    update(deltaTime: number) {
        this.node.getComponent(RigidBody2D).linearVelocity = v2(0,0)
        if(!this.player){
            this.player = find('Canvas/MainLogic/Player')
        }
        if(this.atk){
            this.setani(1)
            this.node.getComponent(RigidBody2D).linearVelocity = this.velocity
            if(this.node.position.x -  this.player.position.x < -2000){
                this.recovery()
            }
        }else{
            this.setani(0)
            if(this.node.position.x -  this.player.position.x <200){
                this.atk = true
            }
        }
        
        

        
    }
}


